package cn.sciento.farm.devicemanager.gateway.model.uplink;

public class LoraWanDataNode {

    private int[] bytes;

    private Integer channelNo;

    private Integer type;

    private Integer data;

    private Integer length = 1;

    public Integer getChannelNo() {
        return channelNo;
    }

    public void setChannelNo(Integer channelNo) {
        this.channelNo = channelNo;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getData() {
        return data;
    }

    public void setData(Integer data) {
        this.data = data;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public int[] getBytes() {
        return bytes;
    }

    public void setBytes(int[] bytes) {
        this.bytes = bytes;
    }

    public void convertData(){
        long data = 0;

        for (int aByte : this.bytes) {
            data = (data << 8) + aByte;
        }
        long maxVal =  1 << (8*this.bytes.length-1);
        if(data > maxVal -1){
            data = data - maxVal*2;
        }
        this.data = (int)data;
    }

    @Override
    public String toString() {
        return "LoraWanDataNode{" +
                "channelNo=" + channelNo +
                ", type=" + type +
                ", data=" + data +
                ", length=" + length +
                '}';
    }
}
